/*
 * @Descripttion:
 * @Author: cuidm
 * @Date: 2022-03-31 14:58:28
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-25 10:53:29
 */
import React, { Fragment, useEffect, useState } from "react";
import { Space, Radio, Spin, Empty, Tooltip, Button} from "antd";
import { PlusCircleOutlined } from '@ant-design/icons';
import request  from "@/utils/request";
import cmdbres from "@/data/cmdbres";
import "./index.less";

const BusinessBtn = ({setBiz , defaultId, history})=>{
	const [data, setData] = useState([]);
    const [loading, setLoading] = useState(false);
    const [defaultValue, setDefaultValue] = useState('');

    useEffect(() => {
        queryList();
    }, []);

    const queryList = async () => {
        try {
            setLoading(true);
            const res = await request({
                ...cmdbres.queryBizList,
                data: {
                    pageNumber:1,
                    pageSize:1000000,
                    sort:{
                        bizCode:"ASC",
                        createTime:"DESC"
                      }
                }
            });
            setLoading(false);
            if (res && res.code == 200) {
                let arr = res.data?.filter(item=> item.cm_unid !=  "0")??[];
                setData(arr);
                if(arr.length) {
                    let dataObj = null;
                    if(defaultId) dataObj = arr.find(obj => obj.cm_unid === defaultId);
                    if(!dataObj) dataObj = arr[0];
                    setBiz(dataObj);
                    setDefaultValue((dataObj.cm_unid));
                }
            }
        } catch (error) {
            setData([]);
            console.log(error);
        }
    }

    const changeBiz = ({ target: { value } }) => {
        const obj = data.find(item=> item.cm_unid == value);
        setBiz(obj);
        setDefaultValue(obj.cm_unid)
    }

    return (
        <div className="business-btn-component">
            <label className="biz-label">
                业务：
            </label>
            {
                loading ? <Spin/>:
                data.length > 0 &&
                <div className="biz-box">
                    <Radio.Group
                        value={defaultValue}
                        onChange={changeBiz}
                        buttonStyle="solid"
                    >
                        {
                            data.map((item, index) =>(
                                <Tooltip title={item.biz_name} placement='top' key={index}>
                                    <Radio.Button value={item.cm_unid} className="name">
                                        {item.biz_name}
                                    </Radio.Button>
                                </Tooltip>
                            ))
                        }
                    </Radio.Group>
                </div>
            }
            <div>
                <Button
                    className="new-btn"
                    type="primary"
                    onClick={() => {
                        history.push({
                            pathname: "/resource/catalog/biz"
                        })
                    }}
                >
                    <PlusCircleOutlined />
                    新建业务
                </Button>
            </div>
        </div>
    );
};

export default BusinessBtn;
